import { useState } from "react";
//封装倒计时
export function useCountdown(time=5,cb){
    const [count,setCount]=useState(time);
    const [isCount,setIsCount]=useState(false);
    //倒计时
    const getCount=()=>{
        setIsCount(true)
        const timer=setInterval(()=>{
            setCount(prev=>{
                if(prev<=1){
                    clearTimeout(timer);
                    cb && cb()
                }
                return prev-1
            })
        },1000)
    }
    const reset=()=>{
        setIsCount(false);
        setCount(time)
    }
    
    return {count,getCount,isCount,reset}
}